package cn.xiaoming.car.server.content.service.imp;

import cn.xiaoming.car.pojo.car.pojo.entity.Car;
import cn.xiaoming.car.pojo.car.pojo.param.CarAddNewParam;
import cn.xiaoming.car.pojo.car.pojo.param.CarUpdateInfoParam;
import cn.xiaoming.car.pojo.car.pojo.vo.CarListItemVO;
import cn.xiaoming.car.pojo.car.pojo.vo.CarVO;
import cn.xiaoming.car.server.ICarService;
import cn.xiaoming.car.server.content.dao.persist.mapper.CarMapper;
import cn.xiaoming.car.server.content.dao.persist.repository.ICarRepository;
import cn.xiaoming.car.server.ex.ServiceException;
import cn.xiaoming.car.server.jsonresult.ServiceCode;
import cn.xiaoming.car.server.pojo.vo.PageData;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@DubboService
public class CarServiceImpl implements ICarService{


    @Value("${xiaoming-car.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    ICarRepository carRepository;
    @Autowired
    CarMapper carMapper;

    @Override
    public void addNew(CarAddNewParam carAddNewParam) {
        log.debug("开始处理【发布车源】的业务，参数：{}", carAddNewParam);

        // TODO 可能需要添加其它业务规则

        String num = carAddNewParam.getPlateNum();
        int count = carRepository.countByNum(num);
        if (count > 0) {
            String message = "发布车源失败，该车牌号车辆已经被发布！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Car car = new Car();
        BeanUtils.copyProperties(carAddNewParam, car);
        car.setId(carAddNewParam.getId());
        int rows = carRepository.insert(car);
        if (rows != 1) {
            String message = "发布车辆失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【下架车辆】的业务，参数：{}", id);

        CarListItemVO currentCar = carRepository.getCarById(id);
        CarVO currentDetail = carRepository.getDetailById(id);
        log.debug("查看参数Car："+currentCar);
        if (currentCar == null || currentCar.getId() == 0) {
            String message = "下架车辆失败，尝试下架的车辆数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int row1 = carRepository.deleteById(id);
        int row2 = carRepository.deleteDetailById(id);
        log.debug("查看参数rows："+row1);
        log.debug("查看参数rows："+row2);
        if (row1 != 1) {
            String message = "下架车辆失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }


    @Override
    public void updateInfoById(CarUpdateInfoParam carUpdateInfoParam) {
        log.debug("开始处理【修改车辆信息】的业务，参数：{}", carUpdateInfoParam);

        Long id = carUpdateInfoParam.getId();
        CarVO currentCar = carRepository.getDetailById(id);
        if (currentCar == null || currentCar.getId() == 0) {
            String message = "修改车辆信息失败，尝试修改的车辆数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String plateNum = carUpdateInfoParam.getPlateNum();
        int count = carRepository.countByNumAndId(id, plateNum);
        if (count > 0) {
            String message = "修改车辆信息失败，车辆名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Car car = new Car();
        BeanUtils.copyProperties(carUpdateInfoParam, car);
        car.setId(carUpdateInfoParam.getId());
        int rows = carRepository.updateById(car);
        if (rows != 1) {
            String message = "修改车辆信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }


    @Override
    public CarListItemVO getCarById(Long id) {
        log.debug("开始处理【根据ID查询车辆】业务，参数：{}",id);
        CarListItemVO carInfo=carRepository.getCarById(id);
        log.debug("查看一下："+carInfo);
        if(carInfo==null||carInfo.getId()==0){
            String message="获取车辆详情失败，尝试访问的车辆数据不存在！";
            log.warn(message);
            throw new SecurityException(String.valueOf(ServiceCode.ERR_NOT_FOUND));
        }
        return carInfo;
    }

    @Override
    public CarVO getDetailById(Long id) {
        log.debug("开始处理【根据ID查询车辆】业务，参数：{}",id);
        CarVO carInfo=carRepository.getDetailById(id);
        log.debug("查看一下："+carInfo);
        if(carInfo==null||carInfo.getId()==0){
            String message="获取车辆详情失败，尝试访问的车辆数据不存在！";
            log.warn(message);
            throw new SecurityException(String.valueOf(ServiceCode.ERR_NOT_FOUND));
        }
        return carInfo;
    }

    @Override
    public void setEnable(Long id) {
        log.debug("开始处理【启用车辆信息】的业务，参数：{}", id);
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Long id) {
        log.debug("开始处理【禁用车辆信息】的业务，参数：{}", id);
        updateEnableById(id, 0);
    }


    @Override
    public PageData<CarListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询车辆列表】，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<CarListItemVO> pageData = carRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<CarListItemVO> list(Integer pageNum) {
        log.debug("开始执行【查询车辆列表】，页码：{}", pageNum);
        PageData<CarListItemVO> pageData = carRepository.list(pageNum,defaultQueryPageSize);
        return pageData;
    }

    private void updateEnableById(Long id, Integer enable) {
        CarListItemVO currentCar = carRepository.getCarById(id);
        if (currentCar == null || currentCar.getId() == 0) {
            String message = ENABLE_TEXT[enable] + "操作失败，尝试访问的车辆数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        if (currentCar.getEnable() == enable) {
            String message = ENABLE_TEXT[enable] + "操作失败，对应信息已经处于此状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Car car = new Car();
        car.setId(id);
        car.setEnable(enable);
        int rows = carRepository.updateById(car);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "操作失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }
}


